<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 800px;
            height: 300px;
            margin: 100px auto;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .box .inp {
            width: 600px;
            height: 200px;

            display: flex;
            align-items: center;
            justify-content: center;
        }

        .box .inp .input-area {
            position: relative;
            height: 50px;
            flex: 7;
        }

        .box .inp .input-area .search-history-list {
            display: none;
            box-sizing: border-box;
            width: 100%;
            height: 400px;
            position: absolute;
            top: 88%;
            left: 0;
            border: 1px solid rgb(155, 128, 255);
            border-top: none;
            background: white;
        }

        .box .inp .input-area .search-history-list .search-list-item {
            padding-left: 10px;
            height: 50px;
            line-height: 50px;
            list-style: none;
        }

        .box .inp .input-area .search-history-list .search-list-item:hover {
            background: rgb(238, 235, 235);
            color: rgb(155, 128, 255);
            cursor: pointer;
        }

        .box .inp .input-area input {
            padding-left: 10px;
            box-sizing: border-box;
            width: 100%;
            height: 100%;
            border: 1px solid rgb(128, 99, 233);
            border-top-left-radius: 10px;
            border-bottom-left-radius: 10px;
            outline: none
        }

        .box .inp .btn-area {
            height: 50px;
            flex: 2;
            background: blue;
            border: 1px solid #ccc;
        }

        .box .inp .btn-area button {
            width: 100%;
            height: 100%;
            border: none;
            background: #05f;
            font-size: 20px;
            font-family: 黑体;
            color: white;
        }
    </style>
</head>

<body>
    <div id="box" class="box">
        <div class="inp">
            <div class="input-area">
                <input id="searchInput" type="text">
                <ul id="historyList" class="search-history-list">
                    <!-- <li class="search-list-item">帅哥皮肤挺黄啊</li> -->
                </ul>
            </div>
            <div class="btn-area">
                <button id="searchBtn">千锋一下</button>
            </div>
        </div>
    </div>
    <script>
        // 获取元素
        var searchInput = document.getElementById("searchInput");
        var historyList = document.getElementById("historyList");
        var searchBtn = document.getElementById("searchBtn");

        // 定义数组 用于存储用户输入的内容
        var arr = JSON.parse(localStorage.getItem("record")) || [];

        // 将数组中的每一条数据渲染成li 并放入页面的搜索历史记录区域
        historyList.innerHTML = arr.map(val => ` <li class="search-list-item">${val}</li>`).join("")
        // 当获取焦点的时候
        searchInput.onfocus = function () {
            historyList.style.display = "block"
        }
        // 当失去焦点的时候
        searchInput.onblur = function () {
            historyList.style.display = "none"
        }

        // 当点击千锋一下的时候
        searchBtn.onclick = function() {
            // 获取输入框的内容并放入本地存储
            var val = searchInput.value;
            // 如果没输入内容 
            if (/^\s*$/.test(val)) {
                return;
            }
            // console.log(val)
            // 把数据放入数组 
            arr.push(val);
            // 把数组放入本地存储
            localStorage.setItem("record", JSON.stringify(arr))
        }

        searchBtn.onmousedown = function() {
            this.style.fontSize = "25px";
        }
        searchBtn.onmouseup = function() {
            this.style.fontSize = "20px";
        }

    </script>
</body>

</html>